package javaInterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javaExecute.Connectar;

public class PesquisaFornecedor {
	
	private JDialog dialog;
	private JTextField textField;
	private JTable table;
	private Statement stmt = null;
	private ResultSet rs;
	
	public PesquisaFornecedor(){
		initialize();
	}
	
	private void initialize() {
		dialog = new JDialog();
		final JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBackground(Color.LIGHT_GRAY);
		
		Dimension tamanhoDaTela = Toolkit.getDefaultToolkit().getScreenSize();
		dialog.setBounds((tamanhoDaTela.width/2)-250, (tamanhoDaTela.height/2)-300, 500, 300);
		dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		dialog.getContentPane().setLayout(null);
		dialog.setResizable(false);

		desktopPane.setBounds(0, 0, dialog.getWidth(),dialog.getHeight());
		dialog.getContentPane().add(desktopPane);
		
		JLabel lblDigiteOProduto = new JLabel(" Digite o produto");
		lblDigiteOProduto.setBounds(10, 11, 130, 14);
		
		textField = new JTextField();
		textField.setBounds(10, 27, 211, 20);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Pesquisar");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				carregaTabela();
			}
		});
		btnNewButton.setBounds(231, 26, 91, 23);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 75, 468, 187);
		desktopPane.setLayout(null);
		desktopPane.add(lblDigiteOProduto);
		desktopPane.add(textField);
		desktopPane.add(btnNewButton);
		desktopPane.add(scrollPane);
		
		String[] columnNames = {"First Name",
                "Last Name",
                "Sport",
                "# of Years",
                "Vegetarian"};

		Object[][] data = {
				{"Kathy", "Smith",
				"Snowboarding", new Integer(5), new Boolean(false)},
				{"John", "Doe",
				"Rowing", new Integer(3), new Boolean(true)},
				{"Sue", "Black",
				"Knitting", new Integer(2), new Boolean(false)},
				{"Jane", "White",
				"Speed reading", new Integer(20), new Boolean(true)},
				{"Joe", "Brown",
				"Pool", new Integer(10), new Boolean(false)}
		};

		final JTable table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		scrollPane.setViewportView(table);
	
	}
	
	public void windowVisible(Boolean option){
		dialog.setVisible(option);
	}
	
	public void carregaTabela(){
		String sql="select * from gateway where date= ";
		try{
			Connectar conn = new Connectar();
			stmt = conn.pegarConexao().createStatement();        
			rs =  stmt.executeQuery(sql);
			System.out.println("CONECTOU");
//			int i=0;
//			while(rs.next()) {
//				Object ids = rs.getString("Business");
//				Object items = rs.getString("GatewayJob");
//				Object descriptions = rs.getString("Status");
//				Object quantitys = rs.getString("Timework");
//				Object price = rs.getString("Notes");      
//				
//				table.getModel().setValueAt(ids,i, 0 );
//				table.getModel().setValueAt(items, i, 1);
//				table.getModel().setValueAt(descriptions, i, 2);
//				table.getModel().setValueAt(quantitys, i, 3);
//				table.getModel().setValueAt(price, i, 4);              
//				System.out.println(i);
//				i++;
//			}
		}catch(SQLException ex){
            System.err.println("SQLException: " + ex.getMessage());
		}
	}
}